package cc.pony.wordcralwer.util;

import java.io.IOException;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class HttpHelper {
	private static HttpClient client = new DefaultHttpClient();
	
	public static String getResponse(HttpGet get){
		try {
			HttpResponse resp = client.execute(get);
			System.out.println("----------------------------------------");  
			System.out.println(resp.getStatusLine());
			Header[] headers =resp.getAllHeaders();
			for(Header header:headers){
				System.out.println(header.getName()+":"+header.getValue());
			}
			System.out.println("----------------------------------------");  		
			HttpEntity entity= resp.getEntity();
			if(entity !=null){
				String string=EntityUtils.toString(entity);
				//String string = new String(EntityUtils.toString(entity).getBytes(),"utf-8");
				//System.out.println(string);
				return string;
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			client.getConnectionManager().shutdown();
		}
		return null;
	}
}
